package com.coocaa.ops.admin.v1.module;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.util.SysAuthPageQueryUtil;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.module.model.ModuleInfoModel;
import com.coocaa.ops.admin.v1.module.service.IModuleInfoQueryService;
import com.coocaa.ops.admin.v1.module.service.IModuleInfoUpdateService;
import com.coocaa.ops.admin.v1.module.validator.ModuleValidator;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.module.entity.ModuleInfoEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author bjh
 * @description  模块信息原始表-前端控制器
 * @date 2020-06-15 16:34:46
 */
@RestController
@Config(menuToken = "module")
@RequestMapping("/v1/module")
public class ModuleInfoController extends MgrBaseController {

    @Autowired
    private IModuleInfoUpdateService thisUpdateService;
    @Autowired
    private IModuleInfoQueryService  thisQueryService;

    /**
     * 保存
     *
     * @param jsonStr
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Object save(@RequestParam String jsonStr,HttpServletRequest request){
        ModuleInfoEntity entity=(ModuleInfoEntity)JsonUtil.json2Object(jsonStr, ModuleInfoEntity.class);
        ModuleValidator.validBeforeSave(entity);
        thisUpdateService.save(entity,WebUtil.getCurrentUser(request));
        return R.ok();

    }

    /**
     * 分页数据显示-菜单自己使用
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String pageList(HttpServletRequest request,  ModuleInfoModel model){
        PageResultModel<ModuleInfoEntity> pageResult=thisQueryService.pageList(model);
        R result=R.ok(pageResult);
        return render2Json(result);

    }

    /**
     * 分页数据显示-其他处选择
     *
     * @param request
     * @param model
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/dataList",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String dataList(HttpServletRequest request,  ModuleInfoModel model){
        if(ArrayUtils.isEmpty(model.getStatusArray())){
            model.setStatusArray(new Integer[]{ResourceStatusEnums.TO_AUDITING.getStatus(), ResourceStatusEnums.ON_LINE.getStatus()});
        }
        PageResultModel<ModuleInfoEntity> pageResult=thisQueryService.dataList(model);
        R result=R.ok(pageResult);
        return render2Json(result);
    }


    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDetailInfo", method = RequestMethod.GET)
    public Object getDetailInfo(@RequestParam Integer id,@RequestParam(required = false) Integer version){
        InputParamsValidator.validIdValue(id);
        ModuleInfoEntity entity=thisQueryService.getDetailInfo(id,version);
        Map result=R.ok().data(entity);;
        return render2Json(result);

    }

    /**
     * 删除记录
     *
     * @param
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove",method = {RequestMethod.GET,RequestMethod.POST})
    public Object remove(Integer[]id,HttpServletRequest request){
        Assert.notEmpty(id,"删除的id不能为空！");
        return thisUpdateService.deleteBatch(id,WebUtil.getCurrentUser(request));

    }

    /**
     * 删除副本
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/delHistory",method = {RequestMethod.GET,RequestMethod.POST})
    public Object delHistory(HttpServletRequest request, Integer id, Integer version) {
        InputParamsValidator.validIdAndVersion(id, version);
        thisUpdateService.deleteHistory(id, version, WebUtil.getCurrentUser(request));
        return R.ok();
    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo",method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }


}
